from django.db import models

class MetricSite(models.Model):
    name = models.CharField(max_length=50, primary_key=True)
    check_mins = models.IntegerField()
    last_checked = models.DateTimeField(null=True, blank=True, default=None)

    def get_set(self, name):
        return self.metricset_set.filter(name=name)[0]

    def __unicode__(self):
        return self.name
        
class MetricSet(models.Model):
    metricsite = models.ForeignKey(MetricSite)
    name = models.CharField(max_length=100, primary_key=True)
    description = models.TextField()

    collection_mins = models.IntegerField(default=60)
    shadow_mins = models.IntegerField(default=0)
    default_history_mins = models.IntegerField(default=0)
    
    earliest_time_collected = models.DateTimeField(null=True, blank=True, default=None)
    last_collected = models.DateTimeField(null=True, blank=True, default=None)

    def get_defn(self, name):
        return self.metricdefn_set.filter(name=name)[0]

    def __unicode__(self):
        return self.name

class MetricDefn(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    metricset = models.ForeignKey(MetricSet)

    class Meta:
        unique_together = [("metricset", "name"),]
    
    def __unicode__(self):
            return "%s:%s" % (self.metricset.name, self.name)
        
class MetricStat(models.Model):
    metricdefn = models.ForeignKey(MetricDefn)
    time_collected = models.DateTimeField(null=False, blank=False)
    value = models.FloatField(null=True)

    class Meta:
        unique_together = [("metricdefn", "time_collected"),]
        
    def __unicode__(self):
            return "%f" % self.value
